<?php

namespace Crm\Controller;

/**
 * 管理员控制器
 *
 * @author SoChishun <14507247@qq.com>
 * @version 1.0
 * @since 1.0.0 <2015-3-24> by SoChishun Added.
 */
class AdminController extends AppbaseController {

    public function assert_upr($rule = '') {
        $upr = array(
            'admin_list' => true,
            'admin_edit' => true,
        );

        $this->user_permission_rules = $upr;
        if ($rule && (!array_key_exists($rule, $upr) || !$upr[$rule])) {
            die('您没有访问权限!');
        }
    }

    /**
     * 返回admin_list查询数据
     * @return array
     * @since 1.0 <2015-3-24> SoChishun Added.
     */
    function admin_list_search() {
        $search = I('get.');
        $where['user_type'] = 'ADMIN';
        if ($search['keyword']) {
            $where['user_name'] = array('like', '%' . $search['keyword'] . '%');
        }
        return array('search' => $search, 'where' => $where);
    }

    /**
     * index操作
     * @since 1.0.0 <2015-3-24> by SoChishun Added.
     */
    public function admin_list() {
        $this->base_common_add_visit_history('管理员管理');
        $this->assert_upr('admin_list');
        $search_data = $this->admin_list_search();
        $list = D('User')->get_paging_list($search_data['where'], $pager);
        $this->assign('list', $list);
        $this->assign('pager', $pager);
        $this->assign('upr', $this->user_permission_rules);
        $this->display_tpp();
    }

    /**
     * 编辑管理员操作
     * @since 1.0 <2015-3-25> SoChishun Added.
     */
    public function admin_edit($id = 0) {
        $this->base_common_add_visit_history($id ? '编辑管理员' : '新增管理员');
        $this->assert_upr('admin_edit');
        // 角色树形列表
        $this->assign('role_list', D('Role')->get_tree_list());
        $this->base_common_edit_view($id, 'User');
    }

    /**
     * 编辑管理员保存操作
     * @since 1.0 <2015-3-25> SoChishun Added.
     */
    public function admin_edit_save() {
        $result = D('User')->save_admin();
        $this->base_redirect_return($result, U('User/admin_list'));
    }

    /**
     * 删除管理员操作
     * @param int $id
     * @since 1.0 <2015-3-25> SoChishun Added.
     */
    public function admin_delete($id = 0) {
        $this->base_common_delete($id, 'User');
    }

}
